/****************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxsupprsapi.p                                    */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : supplier required export Process                 */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                 5-Mar-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i sosodxr}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.

define variable v_shipfrom_from         as character  no-undo.
define variable v_shipfrom_to           as character  no-undo.
define variable v_shipto_from           as character  no-undo.
define variable v_shipto_to             as character  no-undo.
define variable v_part_from             as character  no-undo.
define variable v_part_to               as character  no-undo.
define variable v_site_from             as character  no-undo.
define variable v_site_to               as character  no-undo.
define variable v_order_from            as character  no-undo.
define variable v_order_to              as character  no-undo.
define variable v_current_ind           as logical  initial yes  no-undo.
define variable v_date_from             as date    no-undo.
define variable v_date_to               as date    no-undo.
define variable v_schtype               as integer initial 4   no-undo.
define variable prior_disp_qty         as decimal    no-undo.
define variable net_req               as decimal    no-undo.
define variable cum_plan_qty          as decimal    no-undo.
define variable prior_net_req         as decimal    no-undo.
define variable prior_contr           as character  no-undo.
define variable prior_part            as character  no-undo.
define variable disp_qty              as decimal    no-undo.

define buffer b_schmstr for sch_mstr.
define buffer b_pomstr for po_mstr.
define buffer b_poddet for pod_det.
define buffer b_scxref for scx_ref.

define temp-table ttSearchCondition no-undo

        field supplier_from         like creditor.creditorcode
        field supplier_to           like creditor.creditorcode
        field shipto_from         as character
        field shipto_to           as character
        field part_from             as character
        field part_to               as character
		field site_from             as character
        field site_to               as character
        field order_from            as character
        field order_to              as character
        field schtype               as integer
        field date_from             as date 
        field date_to               as date 
        field active_rls            as logical
        .


 define temp-table ttsupplierrs no-undo

        field schd_site      like si_mstr.si_site
        field schd_order     like scx_ref.scx_order
        field schd_line      like scx_ref.scx_line 
        field schd_vend      like po_mstr.po_vend
        field schd_shipto    like scx_ref.scx_shipto
        field schd_dock      like pod_det.pod_dock
        field schd_part      like pod_det.pod_part
        field schd_vpart     like pod_det.pod_part
        field schd_rlse_id   like sch_mstr.sch_rlse_id
        field schd_date      like schd_det.schd_date
        field schd_time      as   character
        field schd_reference like schd_det.schd_reference
        field schd_ord_mult  like pod_det.pod_ord_mult
        field schd_um        like pod_det.pod_um 
        field schd_pkg_code  like pod_det.pod_pkg_code
        field schd_pcr_qty   like sch_mstr.sch_pcr_qty
        field schd_cumrc_qty   like sch_mstr.sch_pcr_qty
        field schd_interval  like schd_det.schd_interval
        field schd_pcs_date  like sch_mstr.sch_pcs_date
        field schd_scx_po    like scx_ref.scx_po
        field schd_cr_date   like sch_mstr.sch_cr_date
        field schd_cr_time   as   character
        field schd_eff_start like sch_mstr.sch_eff_start
        field schd_eff_end   like sch_mstr.sch_eff_end 
        field schd_fc_qual   like schd_det.schd_fc_qual
        field schd_qty       as   decimal
        field schd_status    as character
       
        

        index idx1 schd_order schd_line schd_date schd_time.

                    

  
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dssupplierrs FOR ttsupplierrs.

   hresult = dataset dssupplierrs:HANDLE.    
                
output to /apps/eai/work/cim/supprs.log.
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "vd/"
       m_archpath    = m_archpath + "vd/"
       m_successpath = m_successpath + "vd/"
       m_errorpath   = m_errorpath + "vd/"
       m_workpath    = m_workpath + "vd/".


   empty temp-table ttsupplierrs.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

    for each ttSearchCondition:

           assign v_shipfrom_from  = ttSearchCondition.supplier_from
                  v_shipfrom_to    = ttSearchCondition.supplier_to    
                  v_shipto_from    = ttSearchCondition.shipto_from   
                  v_shipto_to      = ttSearchCondition.shipto_to    
                  v_part_from      = ttSearchCondition.part_from   
                  v_part_to        = ttSearchCondition.part_to  
                  v_order_from     = ttSearchCondition.order_from  
                  v_order_to       = ttSearchCondition.order_to 
                  v_site_from      = ttSearchCondition.site_from  
                  v_site_to        = ttSearchCondition.site_to  				  
                  v_schtype        = ttSearchCondition.schtype
                  v_date_from      = ttSearchCondition.date_from
                  v_date_to        = ttSearchCondition.date_to
                  v_current_ind    = ttSearchCondition.active_rls
                  . 

     end. /* for each ttSearchCondition. */

         if v_shipfrom_to = "" then v_shipfrom_to = hi_char.
         if v_shipto_to = ""   then v_shipto_to = hi_char.
         if v_part_to = ""     then v_part_to = hi_char.
         if v_order_to = ""    then v_order_to = hi_char.
         if v_site_to = ""     then v_site_to = hi_char.
         if v_date_from = ?    then v_date_from = low_date.
         if v_date_to = ?      then v_date_to = hi_date.
  



         for each scx_ref no-lock where scx_ref.scx_domain = global_domain 
                                    and scx_ref.scx_type = 2
                                    and scx_ref.scx_shipfrom >= v_shipfrom_from 
                                    and scx_ref.scx_shipfrom <= v_shipfrom_to
                                    and scx_ref.scx_shipto >= v_site_from 
                                    and scx_ref.scx_shipto <= v_site_to
                                    and scx_ref.scx_part >= v_part_from 
                                    and scx_ref.scx_part <= v_part_to
                                    and scx_ref.scx_po >= v_order_from 
                                    and scx_ref.scx_po <= v_order_to,
             each pod_det no-lock where pod_det.pod_domain = global_domain 
                                    and pod_det.pod_nbr = scx_ref.scx_order 
                                    and pod_det.pod_line = scx_ref.scx_line,
             each po_mstr no-lock where po_mstr.po_domain = global_domain 
                                    and po_mstr.po_nbr = pod_det.pod_nbr
									
                                    // and po_buyer >= buyer_from 
                                    // and po_buyer <= buyer_to
             break by scx_part by scx_shipto by scx_shipfrom by scx_po:
                if v_current_ind then do:
                    find sch_mstr  where sch_mstr.sch_domain = global_domain 
                                     and sch_type = v_schtype
                                     and sch_nbr = pod_nbr 
                                     and sch_line = pod_line
                                     and sch_cr_date >= v_date_from 
                                     and sch_cr_date <= v_date_to
                                     and sch_rlse_id = pod_curr_rlse_id[v_schtype - 3]
                                     no-lock no-error.
                    if available sch_mstr then do:
                        //    {us/bbi/gprun.i ""rsiq1a.p"" "(input schtype,
                        //           input pod_nbr, input pod_line, input sch_rlse_id,
                        //           input no, input yes)"}
                    find b_schmstr where b_schmstr.sch_domain = global_domain 
                                     and b_schmstr.sch_type = v_schtype
                                     and b_schmstr.sch_nbr = sch_mstr.sch_nbr
                                     and b_schmstr.sch_line = sch_mstr.sch_line
                                     and b_schmstr.sch_rlse_id = sch_mstr.sch_rlse_id
                                     no-lock.
                    find b_pomstr where b_pomstr.po_domain = global_domain 
                                    and b_pomstr.po_nbr = b_schmstr.sch_nbr
                                    no-lock.
                    find b_poddet  where b_poddet.pod_domain = global_domain 
                                     and b_poddet.pod_nbr = b_schmstr.sch_nbr 
                                     and b_poddet.pod_line = b_schmstr.sch_line 
                                     no-lock.
                    find b_scxref  where b_scxref.scx_domain = global_domain 
                                     and b_scxref.scx_type = 2 
                                     and b_scxref.scx_order = b_poddet.pod_nbr
                                     and b_scxref.scx_line = b_poddet.pod_line 
                                     no-lock.
                    find si_mstr  where si_mstr.si_domain = global_domain 
                                    and si_site = b_scxref.scx_shipto 
                                    no-lock.
                    find ad_mstr  where ad_mstr.ad_domain = global_domain 
                                    and ad_addr = b_scxref.scx_shipfrom 
                                    no-lock.
                    find vd_mstr  where vd_mstr.vd_domain = global_domain 
                                    and vd_addr = b_pomstr.po_vend
                                    no-lock.
                    find pt_mstr  where pt_mstr.pt_domain = global_domain 
                                    and pt_part = b_scxref.scx_part 
                                    no-lock no-error.
                                 
                    // if show_net_req then detail_frame_title = detail_frame_titles[1].
                    //                 else
                    // if sch_cumulative then detail_frame_title = detail_frame_titles[2].
                    //                 else detail_frame_title = detail_frame_titles[3].
                                 
                                    
                    for each schd_det no-lock where schd_domain  = global_domain
                                                and schd_type    = b_schmstr.sch_type
                                                and schd_nbr     = b_schmstr.sch_nbr
                                                and schd_line    = b_schmstr.sch_line
                                                and schd_rlse_id = b_schmstr.sch_rlse_id
                                                break by schd_date:

                    if b_schmstr.sch_cumulative then disp_qty = schd_det.schd_cum_qty.
                                           else disp_qty = schd_det.schd_discr_qty.
                    
                     create ttsupplierrs.  
                                                    
                     assign                 
                            ttsupplierrs.schd_site       =  b_scxref.scx_shipfrom
                            ttsupplierrs.schd_order      =  b_scxref.scx_po                        
                            ttsupplierrs.schd_line       =  b_scxref.scx_line  
                            ttsupplierrs.schd_vend       =  b_pomstr.po_vend     
                            ttsupplierrs.schd_shipto     =  b_scxref.scx_shipto                           
                            ttsupplierrs.schd_part       =  b_scxref.scx_part               
                            ttsupplierrs.schd_vpart      =  b_poddet.pod_vpart                
                            ttsupplierrs.schd_rlse_id    =  b_schmstr.sch_rlse_id                        
                            ttsupplierrs.schd_date       =  schd_det.schd_date               
                            ttsupplierrs.schd_time       =  string(schd_det.schd_time,"HH:MM:SS")                      
                            ttsupplierrs.schd_reference  =  schd_det.schd_reference                           
                            ttsupplierrs.schd_um         =  pod_det.pod_um                                        
                            ttsupplierrs.schd_cumrc_qty  =  pod_det.pod_cum_qty[1]                                        
                            ttsupplierrs.schd_pcr_qty    =  b_schmstr.sch_pcr_qty               
                            ttsupplierrs.schd_interval   =  schd_det.schd_interval                  
                            ttsupplierrs.schd_pcs_date   =  b_schmstr.sch_pcs_date                                              
                            ttsupplierrs.schd_cr_date    =  b_schmstr.sch_cr_date                       
                            ttsupplierrs.schd_cr_time    =  string(b_schmstr.sch_cr_time,"HH:MM:SS")                      
                            ttsupplierrs.schd_eff_start  =  b_schmstr.sch_eff_start                       
                            ttsupplierrs.schd_eff_end    =  b_schmstr.sch_eff_end                       
                            ttsupplierrs.schd_fc_qual    =  schd_det.schd_fc_qual                              
                            ttsupplierrs.schd_qty        =   disp_qty       
                            ttsupplierrs.schd_status     =  "SUCCESS"                       
                            .
                                      
                                    
                 
                    end. /* for each schd_det  */
                    end. /* if available sch_mstr then do: */
                 end. /*  if  current_ind then do: */





                /* 全部日程 包含无效 */
                //  else do:
                //     for each sch_mstr no-lock
               
                //      where sch_mstr.sch_domain = global_domain and  sch_type = schtype
                //     and sch_nbr = pod_nbr and sch_line = pod_line
                //     and sch_cr_date >= date_from and sch_cr_date <= date_to
                //     by sch_cr_date by sch_cr_time:
                //           {us/bbi/gprun.i ""rsiq1a.p"" "(input schtype,
                //                    input sch_nbr, input sch_line, input sch_rlse_id,
                //                    input no, input yes)"}
                
                //     end.
                //  end.



      end.  /* for each scx_ref no-lock where scx_ref.scx_domain = global_domain */
 

  end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
